/**
 * Created by ACER on 2018/1/27.
 */
app.controller("user-register",function($http,$scope){

    //用户邮箱
    $scope.email = '';
    //用户登录密码
    $scope.password = '';
    //验证码
    $scope.verificationCode='';
    $scope.emailRegister = function(){
        var registerInfo = {'email':$scope.email,'password':$scope.password,'verificationCode':$scope.verificationCode};
        $http({
            method:'post',
            url:app.netPath+'/users/register/email',
            params:registerInfo
        }).then(function(res){
            if(!res.data.error){
                $.toast('注册成功');
                //成功后自动登录
                var loginInfo={'username':$scope.email,'password':$scope.password};
                $http({
                    method: 'post',
                    url: app.netPath+'/users/login',
                    params: loginInfo
                }).then(function(res){
                    console.log(res);
                    if(!res.data.error){
                        localStorage.setItem('token',res.data.data);
                        location.href='#personal-center';
                    }
                    else {
                        $.toast(res.data.msg);
                    }
                },function(){
                    $.toast('网络或设置错误');
                });
                location.href='#login'

            }else{
                var error=res.data.msg;
                $.toast(error)
            }
        },function(){
            $.toast('网络走丢了！')
        })
    };
    $scope.getVerificationCode=function(){
        var email={'email':$scope.email};
        $http({
            method:'get',
            url:app.netPath+'/users/getVerificationCode',
            params:email
        }).then(function(res){
            if(!res.data.error){
                $.toast('验证码发送成功');
            }else{
                $.toast(res.data.msg)
            }
        },function(res){
            console.log(res);
            $.alert('网络有问题哟');
        })
    }
});